<?php

require('../include/init.inc.php');
$user_name = $password = $remember = $verify_code = '';
extract($_POST, EXTR_IF_EXISTS);

//$result = User::updateUser(1, array('password' => md5('111111')));

$cookieInfo = User::getCookieRemember();//每次进来先检查cookie里面有没有，有就直接进入，没有再跳转到登录界面
if ($cookieInfo) {
    $user_info = User::getUserById($cookieInfo);
    if ($user_info) {
        if ($user_info['status'] == 1) {
            User::loginDoSomething($user_info['user_id']);
            $ip = Common::getIp();
            SysLog::addLog($user_name, 'LOGIN', 'User', UserSession::getUserId(), json_encode(array("IP" => $ip)));
            Common::jumpUrl('panel/index.php');
        } else {
            OSAdmin::alert("error", ErrorMessage::BE_PAUSED);
        }
    }
}


if (Common::isPost()) {
    //不要验证码，安全性不高
//    if (strtolower($verify_code) != strtolower($_SESSION['osa_verify_code'])) {
//        OSAdmin::alert("error", ErrorMessage::VERIFY_CODE_WRONG);
//    } else {


    $user_info = User::checkPassword($user_name, $password);

    if ($user_info) {
        if ($user_info['status'] == 1) {

            User::loginDoSomething($user_info['user_id']);

            if ($remember) {
                $encrypted = OSAEncrypt::encrypt($user_info['user_id']);
                User::setCookieRemember(urlencode($encrypted), 30);
            }
            $ip = Common::getIp();
            SysLog::addLog($user_name, 'LOGIN', 'User', UserSession::getUserId(), json_encode(array("IP" => $ip)));
            Common::jumpUrl('panel/index.php');
        } else {
            OSAdmin::alert("error", ErrorMessage::BE_PAUSED);
        }
    } else {
        OSAdmin::alert("error", ErrorMessage::USER_OR_PWD_WRONG);
        SysLog::addLog($user_name, 'LOGIN', 'User', '', json_encode(ErrorMessage::USER_OR_PWD_WRONG));
    }
//    }
}

Template::assign('_POST', $_POST);
Template::assign('page_title', '登入');
Template::Display('login.tpl');